<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 众筹管理
 */
class Fund extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('fund_model');
        
    }
    //信用卡列表
    public function index()
    {
        $where = array();
        if(isset($_REQUEST['key']) && trim($_REQUEST['key'])){
            $keyword = trim($_REQUEST['key']);
            $where['fund.title'] = $keyword;
        }
        $getcount = $this->fund_model->get_count($where);
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $list = $this->fund_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
        foreach ($list as $key=>$val){
        }
        $this->template->assign('list',$list);
        $this->template->assign('Total',$getcount);
        $this->template->assign('keyname',$keyword);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('segment_p','index');
        $this->template->load('fund/index');
    }
    
    public function fund_add(){
        if($_POST){
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['introduction'] = trim($_POST['introduction']);
            $data['description'] = trim($_POST['description']);
            $data['website'] = trim($_POST['website']);
            $data['promlink'] = trim($_POST['promlink']);
            $data['banner_list'] = trim($_POST['banner_list']);
            $data['banner_detail'] = trim($_POST['banner_detail']);
            $data['app_banner_link'] = trim($_POST['app_banner_link']);
            $data['pro_industry'] = trim($_POST['pro_industry']);
            $data['develop_stage'] = trim($_POST['develop_stage']);
            $data['team_nums'] = trim($_POST['team_nums']);
            $data['organizer'] = trim($_POST['organizer']);
            $data['organizer_head'] = trim($_POST['organizer_head']);
            $data['company_logo'] = trim($_POST['company_logo']);
            $data['company_name'] = trim($_POST['company_name']);
            $data['company_address'] = trim($_POST['company_address']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_show'] = trim($_POST['is_show']);
            $data['create_time'] = time();
            
            $result = $this->fund_model->insert($data);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url("fund/index");
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url("fund/index");
                echo json_encode($mssage);exit;
            }
        }
        
        $this->template->assign('segment_p','index');
        $this->template->load('fund/fund_add');
    }
    
    public function fund_edit(){
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['introduction'] = trim($_POST['introduction']);
            $data['description'] = trim($_POST['description']);
            $data['website'] = trim($_POST['website']);
            $data['promlink'] = trim($_POST['promlink']);
            $data['banner_list'] = trim($_POST['banner_list']);
            $data['banner_detail'] = trim($_POST['banner_detail']);
            $data['app_banner_link'] = trim($_POST['app_banner_link']);
            $data['pro_industry'] = trim($_POST['pro_industry']);
            $data['develop_stage'] = trim($_POST['develop_stage']);
            $data['team_nums'] = trim($_POST['team_nums']);
            $data['organizer'] = trim($_POST['organizer']);
            $data['organizer_head'] = trim($_POST['organizer_head']);
            $data['company_logo'] = trim($_POST['company_logo']);
            $data['company_name'] = trim($_POST['company_name']);
            $data['company_address'] = trim($_POST['company_address']);
            $data['sort'] = trim($_POST['sort']);
            $data['is_show'] = trim($_POST['is_show']);
            
            $result = $this->fund_model->update($data,$id);
            if($result){
                $mssage['info']	  = '更新成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url("fund/index");
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '更新失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url("fund/index");
                echo json_encode($mssage);exit;
            }
        }
        
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        $item = $this->fund_model->get(array("id"=>$id));
        if(!$item){
            redirect("credit/index");
        }
        
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','index');
        $this->template->load('fund/fund_edit');
    }
    
    function setshow(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_show = trim($_GET['is_show']);
            $data = array();
            $data['is_show'] = $is_show;
            if($data['is_show'] == 0){
                $data['is_recommand'] = 0;
            }
            $result = $this->fund_model->update($data,$id);
            if($result){
                if($is_show==1){
                    $mssage['info']	  = '显示成功';
                }else{
                    $mssage['info']	  = '隐藏成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    
    function setrecommand(){
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['id']);
            $is_recommand = trim($_GET['is_recommand']);
            $data = array();
            $data['is_recommand'] = $is_recommand;
            $result = $this->fund_model->update($data,$id);
            if($result){
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
}
